//一般直接写在一个js文件中
layui.use(['element','form','layer','laytpl','laydate'],function () {
    var element = layui.element,
            laydate = layui.laydate;
    laydate.render({
        elem: '#endTime' //指定元素
    });

    //选择老人
    $("#elderlyName").on('click',function () {
        layer.open({
            type: 2,
            content: '/mbassess/select/elderly',
            area: ['800px', '600px'],
            title:'选择老人',
            btn: ['确定 '],
            yes:function(index, layero){
                var iframeWin = window[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象
                var checkStatus = iframeWin.layui.table.checkStatus('mytable');
                if(checkStatus.data.length < 1 || checkStatus.data.length > 1){
                    iframeWin.layui.layer.msg("请选择一位老人");
                    return false;
                }
                var name = checkStatus.data[0].name;
                var id = checkStatus.data[0].id;
                var idNo = checkStatus.data[0].idNo;
                $("#elderlyName").val(name);
                $("#elderlyId").val(id);
                $("#elderlyIdNo").val(idNo);
               $("#jibingForm")[0].reset();
                $("#richangForm")[0].reset();
                $("#jingshenForm")[0].reset();
                $("#ganzhiForm")[0].reset();
                $("#shehuiForm")[0].reset();
                $("#pgId").val("");
                $("#cause option:selected").val("");
                element.tabChange('demo', 'one');
                //获取老人基本信息
                $("#pgbh").val(id);
                var now = new Date();
                var time = now.getFullYear() + "年" +((now.getMonth()+1)<10?"0":"")+(now.getMonth()+1)+"月"+(now.getDate()<10?"0":"")+now.getDate()+"日";
                $("#todayDate").text(time);
                $("#ename").text(checkStatus.data[0].name);
                $("#sex").text(checkStatus.data[0].sex);
                $("#birthday").text($.dateFormat(new Date(checkStatus.data[0].birthday), 'yyyy-MM-dd'));
                $("#IdNo").text(idNo);
                $("#ybnum").text(checkStatus.data[0].ybnum=="" ? "不详" : checkStatus.data[0].ybnum);
                $("#nation").text(checkStatus.data[0].nation =="" ? "不详" : checkStatus.data[0].nation);
                if (checkStatus.data[0].education==1){
                    $("#education").text("文盲");
                }else if (checkStatus.data[0].education==2){
                    $("#education").text("小学");
                } else if (checkStatus.data[0].education==3){
                    $("#education").text("初中");
                }else if (checkStatus.data[0].education==4){
                    $("#education").text("高中/技校/中专");
                }else if (checkStatus.data[0].education==5){
                    $("#education").text("大学专科及以上");
                }else{
                    $("#education").text("不详");
                }
                if (checkStatus.data[0].faith==1){
                    $("#faith").text("有");
                }else{
                    $("#faith").text("无");
                }
                if (checkStatus.data[0].wedding==1){
                    $("#wedding").text("已婚");
                }else if (checkStatus.data[0].wedding==2){
                    $("#wedding").text("未婚");
                }else if (checkStatus.data[0].wedding==3){
                    $("#wedding").text("丧偶");
                }else if (checkStatus.data[0].wedding==4){
                    $("#wedding").text("离异");
                }else{
                    $("#wedding").text("未说明的婚姻状况");
                }
                //原居主情况1.独居2与配偶/伴侣居住3与子女居住4与父母居住5与兄弟姐妹居6与其他亲属居住7与非亲属关系的人居住8养老机构
                if (checkStatus.data[0].support==1){
                    $("#support").text("独居");
                }else if (checkStatus.data[0].support==2){
                    $("#support").text("与配偶/伴侣居住");
                }else if (checkStatus.data[0].support==3){
                    $("#support").text("与子女居住");
                }else if (checkStatus.data[0].support==4){
                    $("#support").text("与父母居住");
                }else if (checkStatus.data[0].support==5){
                    $("#support").text("与兄弟姐妹居");
                }else if (checkStatus.data[0].support==6){
                    $("#support").text("与其他亲属居住");
                }else if (checkStatus.data[0].support==7){
                    $("#support").text("与非亲属关系的人居住");
                }else if (checkStatus.data[0].support==8){
                    $("#support").text("养老机构");
                }
                //支付方式1城镇职工基本医疗保险2城镇居住基本医疗保险3新型农村合作医疗4贫困救助5商业医疗保险6全公费7全自费8其它
                if (checkStatus.data[0].zfstatus==1){
                    $("#zfstatus").text("城镇职工基本医疗保险");
                }else if (checkStatus.data[0].zfstatus==2){
                    $("#zfstatus").text("城镇居住基本医疗保险");
                }else if (checkStatus.data[0].zfstatus==3){
                    $("#zfstatus").text("新型农村合作医疗");
                }else if (checkStatus.data[0].zfstatus==4){
                    $("#zfstatus").text("贫困救助");
                }else if (checkStatus.data[0].zfstatus==5){
                    $("#zfstatus").text("商业医疗保险");
                }else if (checkStatus.data[0].zfstatus==6){
                    $("#zfstatus").text("全公费");
                }else if (checkStatus.data[0].zfstatus==7){
                    $("#zfstatus").text("全自费");
                }else if (checkStatus.data[0].zfstatus==8){
                    $("#zfstatus").text("其他");
                }
                //经费来源 1退休金/养老金2子女补贴3亲友资助4其它补贴
                if (checkStatus.data[0].fmoney==1){
                    $("#fmoney").text("退休金/养老金");
                }else if (checkStatus.data[0].fmoney==2){
                    $("#fmoney").text("子女补贴");
                }else if (checkStatus.data[0].fmoney==3){
                    $("#fmoney").text("亲友资助");
                }else if (checkStatus.data[0].fmoney==4){
                    $("#fmoney").text("其它补贴");
                }else {
                    $("#fmoney").text("不详");
                }
                $("#impDisease").text(checkStatus.data[0].impDisease==null ? "无" : checkStatus.data[0].impDisease);
                layer.close(index);

            }
        });
    });

    element.on('tab(demo)', function(data){
        var elderlyId= $("#elderlyId").val();
        var cause= $("#cause option:selected").val();
        if (elderlyId=="" || cause==""){
            if(data.index != 0){
                layer.msg("请选择老人和评估原因");
                element.tabChange('demo', 'one');
            }
        }
        if (elderlyId!="" && cause!=""){
            if(data.index == 1 || data.index == 2 || data.index == 3 || data.index == 4 || data.index == 5){
                if (data.index == 1) {
                    $("#xuanfu").hide();
                    $("#xuanfu2").hide();
                    $("#xuanfu1").show();
                }else if (data.index == 2) {
                    $("#xiangmu").text("日常生活活动总分 :");
                    $("#xiangmufj").text("日常生活活动分级 :");
                    $("#xfone").text("总分 100 分");
                    $("#xftwo").text("总分 65-95 分");
                    $("#xfthree").text("总分 45-60 分");
                    $("#xffour").text("总分≤40 分");
                    $("#xuanfu1").hide();
                    $("#xuanfu2").hide();
                    $("#xuanfu").show();
                    $('#richangForm').trigger("click");
                }else if (data.index == 3) {
                    $("#xiangmu").text("精神状态总分 :");
                    $("#xiangmufj").text("精神状态分级 :");
                    $("#xfone").text("总分为 0 分");
                    $("#xftwo").text("总分为 1 分");
                    $("#xfthree").text("总分 2-3 分");
                    $("#xffour").text("总分 4-6 分");
                    $("#xuanfu1").hide();
                    $("#xuanfu2").hide();
                    $("#xuanfu").show();
                    $('#jingshenForm').trigger("click");
                }else if (data.index == 4){
                    $("#xuanfu").hide();
                    $("#xuanfu1").hide();
                    $("#xuanfu2").show();
                }else if (data.index == 5){
                    $("#xiangmu").text("社会参与总分 :");
                    $("#xiangmufj").text("社会参与分级 :");
                    $("#xfone").text("总分 0-2 分");
                    $("#xftwo").text("总分 3-7 分");
                    $("#xfthree").text("总分 8-13 分");
                    $("#xffour").text("总分 14-20 分");
                    $("#xuanfu1").hide();
                    $("#xuanfu2").hide();
                    $("#xuanfu").show();
                    $('#shehuiForm').trigger("click");
                }
            }else{
                $("#xuanfu1").hide();
                $("#xuanfu").hide();
                $("#xuanfu2").hide();
            }
            if (data.index == 6){
                var id=$("#pgId").val();
                if (id==""){
                    layer.msg("请确认提交所有评估项");
                    return false;
                }
                $.ajax({
                    url:"/mbassess/selectPgbg",
                    dataType:"json",
                    type:"post",
                    data:{"id":id},
                    success:function(rs){
                        if(rs.code == 200 ){
                            var flag   = rs.data;
                            if (flag.rs==null){
                                layer.msg("请确认提交所有评估项");
                                return false;
                            }else{
                                $("#richnagpg").text( flag.dailyLivingGrade+"级");
                                $("#jingshenpg").text( flag.mindGrade+"级");
                                $("#ganzhipg").text( flag.perceptionGrade+"级");
                                $("#shehuipg").text( flag.socialGrade+"级");
                                var chubupd=flag.chubupd==0?"0能力完好":(flag.chubupd==1?"1轻度失能":(flag.chubupd==2?"2中度失能":"3重度失能 "));
                               $("#chubupg").text(chubupd);
                               var biangeng="";
                               if (flag.yishi==3){
                                   biangeng="处于昏迷状态，直接评定为重度失能；"
                               }else if (flag.stupid>0 || flag.spirit>0){
                                   biangeng="有认知障碍/痴呆、精神疾病者;"
                               }else if (flag.fall>1 || flag.lost>1 || flag.foraging>1 || flag.suicide>1){
                                   biangeng="近 30 天内发生过 2 次及以上跌倒、噎食、自杀、走失;"
                               }
                               $("#biangeng").text(biangeng);
                                var rs=flag.rs==0?"0能力完好":(flag.rs==1?"1轻度失能":(flag.rs==2?"2中度失能":"3重度失能 "));
                                $("#zuizhongpg").text(rs);
                                $("#pgbg").show();
                            }
                        }
                    }
                });
            }
        }
    });
    //疾病意外选项监听
    $("#jibingForm").on('click',function () {
        var chidai=$("input[name='stupid']:checked").val();
        var jingshen=$("input[name='spirit']:checked").val();
        var diedao=$("input[name='fall']:checked").val();
        var zoushi=$("input[name='lost']:checked").val();
        var yeshi=$("input[name='foraging']:checked").val();
        var zisha=$("input[name='suicide']:checked").val();
        if (chidai>0 || jingshen>0 && diedao<2 && zoushi<2 && yeshi<2 && zisha<2 ){
            $("#score1").text("有认知障碍/痴呆、精神疾病者，在原有能力级别上提高一个等级；");
        }else  if (diedao>1 || zoushi>1 || yeshi>1 || zisha>1 ){
            $("#score1").text("近 30 天内发生过 2 次及以上跌倒、噎食、自杀、走失者，在原有能力级别上提高一个等级；");
        }else{
            $("#score1").text("无");
        }
    })
    //日常生活评估选项监听
    $("#richangForm").on('click',function () {
        var jinshi=$("input[name='jinshi']:checked").val()==undefined?0:$("input[name='jinshi']:checked").val();
        var xizao=$("input[name='xizao']:checked").val()==undefined?0:$("input[name='xizao']:checked").val();
        var xiushi=$("input[name='xiushi']:checked").val()==undefined?0:$("input[name='xiushi']:checked").val();
        var chuanyi=$("input[name='chuanyi']:checked").val()==undefined?0:$("input[name='chuanyi']:checked").val();
        var dabian=$("input[name='dabian']:checked").val()==undefined?0:$("input[name='dabian']:checked").val();
        var xiaobian=$("input[name='xiaobian']:checked").val()==undefined?0:$("input[name='xiaobian']:checked").val();
        var ruce=$("input[name='ruce']:checked").val()==undefined?0:$("input[name='ruce']:checked").val();
        var zychuangyi=$("input[name='zychuangyi']:checked").val()==undefined?0:$("input[name='zychuangyi']:checked").val();
        var pdfoot=$("input[name='pdfoot']:checked").val()==undefined?0:$("input[name='pdfoot']:checked").val();
        var sxfloor=$("input[name='sxfloor']:checked").val()==undefined?0:$("input[name='sxfloor']:checked").val();
        var score=Number(jinshi)+Number(xizao)+Number(xiushi)+Number(chuanyi)+Number(dabian)+Number(xiaobian)+Number(ruce)+Number(zychuangyi)+Number(pdfoot)+Number(sxfloor);
        $("#score").text(score);
        if (score>95){
            $("#pjresult").text("能力完好");
        }else if (score>=65 && score<= 95){
            $("#pjresult").text("轻度受损");
        } else if (score>=45 && score<= 60){
            $("#pjresult").text("中度受损");
        }else if (score<= 40){
            $("#pjresult").text("重度受损");
        }
    })
    //精神评估选项监听
    $("#jingshenForm").on('click',function () {
        var renzhi=$("input[name='renzhi']:checked").val()==undefined?0:$("input[name='renzhi']:checked").val();
        var kill=$("input[name='kill']:checked").val()==undefined?0:$("input[name='kill']:checked").val();
        var yiyu=$("input[name='yiyu']:checked").val()==undefined?0:$("input[name='yiyu']:checked").val();
        var score=Number(renzhi)+Number(kill)+Number(yiyu);
        $("#score").text(score);
        if (score==0){
            $("#pjresult").text("能力完好");
        }else if (score== 1){
            $("#pjresult").text("轻度受损");
        } else if (score==1 || score== 3){
            $("#pjresult").text("中度受损");
        }else if (score>3){
            $("#pjresult").text("重度受损");
        }
    })
    //感知觉与沟通评估选项监听
    $("#ganzhiForm").on('click',function () {
        var yishi=$("input[name='yishi']:checked").val()==undefined?0:$("input[name='yishi']:checked").val();
        var shili=$("input[name='shili']:checked").val()==undefined?0:$("input[name='shili']:checked").val();
        var tingli=$("input[name='tingli']:checked").val()==undefined?0:$("input[name='tingli']:checked").val();
        var gotong=$("input[name='gotong']:checked").val()==undefined?0:$("input[name='gotong']:checked").val();

        if (yishi==0 && (shili <=1 || tingli <=1 ) &&  gotong==0){
            $("#score2").text("0");
            $("#pjresult2").text("能力完好");
        }else if (yishi==0 &&(shili==2 || tingli==2 || gotong==1)){
            $("#score2").text("1");
            $("#pjresult2").text("轻度受损");
        } else if ((yishi==0  && (shili==3 || tingli==3 || gotong==2)) || (yishi==1 && (shili<=3 || tingli<=3) && gotong<=2 )){
            $("#score2").text("2");
            $("#pjresult2").text("中度受损");
        }else if ((yishi<=1 && (shili==4 || tingli==4 || gotong==3))|| yishi>=2 ){
            $("#score2").text("3");
            $("#pjresult2").text("重度受损");
        }
    })
    //社会参与评估选项监听
    $("#shehuiForm").on('click',function () {
        var shenghuo=$("input[name='shenghuo']:checked").val()==undefined?0:$("input[name='shenghuo']:checked").val();
        var job=$("input[name='job']:checked").val()==undefined?0:$("input[name='job']:checked").val();
        var skdingxiang=$("input[name='skdingxiang']:checked").val()==undefined?0:$("input[name='skdingxiang']:checked").val();
        var rendingxiang=$("input[name='rendingxiang']:checked").val()==undefined?0:$("input[name='rendingxiang']:checked").val();
        var shehuinl=$("input[name='shehuinl']:checked").val()==undefined?0:$("input[name='shehuinl']:checked").val();
        var score=Number(shenghuo)+Number(job)+Number(skdingxiang)+Number(rendingxiang)+Number(shehuinl);
        $("#score").text(score);
        if (score<=2){
            $("#pjresult").text("能力完好");
        }else if (score>= 3 && score<= 7){
            $("#pjresult").text("轻度受损");
        } else if (score>=8 && score<=13){
            $("#pjresult").text("中度受损");
        }else if (score>13){
            $("#pjresult").text("重度受损");
        }
    })
//疾病保存或修改
    $("#jibingBtn").on('click',function () {
        var param=$("#jibingForm").serialize()+"&elderlyId="+ $("#elderlyId").val()+"&elderlyIdNo="+$("#elderlyIdNo").val()+"&elderlyName="+$("#elderlyName").val()+"&cause="+$("#cause option:selected").val()+"&id="+$("#pgId").val();
        if ($("input[name='tgName']").val()!="" && (/^[1][0-9]{10}$/).test($("input[name='tgPhone']").val())){
            $.ajax({
                url:"/mbassess/update",
                dataType:"json",
                type:"post",
                data:param,
                success:function(rs){
                    if(rs.code == 200 ){
                        var flag   = rs.data;
                        layer.msg("保存成功", {time: 1000},function () {
                            $("#pgId").val(flag.id);
                        });
                    }
                }
            });
        }
    })

    $("#richangBtn").on('click',function () {
        var params=$("#richangForm").serialize();
        var arr=params.split('&');
        if (arr.length!=10){
            layer.msg("有未选项");
            return false;
        }
        var param=$("#richangForm").serialize()+"&elderlyId="+ $("#elderlyId").val()+"&elderlyIdNo="+$("#elderlyIdNo").val()+"&elderlyName="+$("#elderlyName").val()+"&cause="+$("#cause option:selected").val()+"&id="+$("#pgId").val();
        $.ajax({
            url:"/mbassess/update",
            dataType:"json",
            type:"post",
            data:param,
            success:function(rs){
                if(rs.code == 200 ){
                    var flag   = rs.data;
                    layer.msg("保存成功", {time: 1000},function () {
                        $("#pgId").val(flag.id);
                    });
                }
            }
        });
    })

    $("#jingshenBtn").on('click',function () {
        var params=$("#jingshenForm").serialize();
        var arr=params.split('&');
        if (arr.length!=3){
            layer.msg("有未选项");
            return false;
        }
        var param=$("#jingshenForm").serialize()+"&elderlyId="+ $("#elderlyId").val()+"&elderlyIdNo="+$("#elderlyIdNo").val()+"&elderlyName="+$("#elderlyName").val()+"&cause="+$("#cause option:selected").val()+"&id="+$("#pgId").val();
        $.ajax({
            url:"/mbassess/update",
            dataType:"json",
            type:"post",
            data:param,
            success:function(rs){
                if(rs.code == 200 ){
                    var flag   = rs.data;
                    layer.msg("保存成功", {time: 1000},function () {
                        $("#pgId").val(flag.id);
                    });
                }
            }
        });
    })

    $("#ganzhiBtn").on('click',function () {
        var params=$("#ganzhiForm").serialize();
        var arr=params.split('&');
        if (arr.length!=4){
            layer.msg("有未选项");
            return false;
        }
        var param=$("#ganzhiForm").serialize()+"&elderlyId="+ $("#elderlyId").val()+"&elderlyIdNo="+$("#elderlyIdNo").val()+"&elderlyName="+$("#elderlyName").val()+"&cause="+$("#cause option:selected").val()+"&id="+$("#pgId").val();
        $.ajax({
            url:"/mbassess/update",
            dataType:"json",
            type:"post",
            data:param,
            success:function(rs){
                if(rs.code == 200 ){
                    var flag   = rs.data;
                    layer.msg("保存成功", {time: 1000},function () {
                        $("#pgId").val(flag.id);
                    });
                }
            }
        });
    })

    $("#shehuiBtn").on('click',function () {
        var params=$("#shehuiForm").serialize();
        var arr=params.split('&');
        if (arr.length!=5){
            layer.msg("有未选项");
            return false;
        }
        var param=$("#shehuiForm").serialize()+"&elderlyId="+ $("#elderlyId").val()+"&elderlyIdNo="+$("#elderlyIdNo").val()+"&elderlyName="+$("#elderlyName").val()+"&cause="+$("#cause option:selected").val()+"&id="+$("#pgId").val();
        $.ajax({
            url:"/mbassess/update",
            dataType:"json",
            type:"post",
            data:param,
            success:function(rs){
                if(rs.code == 200 ){
                    var flag   = rs.data;
                    layer.msg("保存成功", {time: 1000},function () {
                        $("#pgId").val(flag.id);
                    });
                }
            }
        });
    })
    $("#dy").on('click',function(){
        var $print = $("#pgbg");
        $print.find(".hulve").hide();
        $print.jqprint();
        $print.find(".hulve").show();
    });
});
